home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / split.cp < prev    next >
Encoding:
Text File  |  1995-04-05  |  5.8 KB  |  82 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    plane.cp
  3. //    Date:                    3/26/95
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the method for splitting a polygon
  7. //                                with a plane.
  8. //
  9. //------------------------------------------------------------------------------
  10.  
  11. #include "split.h"
  12.  
  13. //------------------------------------------------------------------------------
  14. //    plane the polygon with a plane_3d
  15. //------------------------------------------------------------------------------
  16. hclass    Split (const polyptr &poly, const plane_3d &plane, polyptr &in, polyptr &out)//    split the polygon with a plane_3d
  17. {                                                                                                                                                                //    begin
  18.     static    point_3d    outpts[64],                                                                                                    //    an array of points for the out polygon
  19.                                         inpts[64];                                                                                                    //    an array of points for the in polygon
  20.     int                out_c = 0, in_c = 0;                                                                                                //    set the point_3d counts to 0
  21.     point_3d    ptA, ptB;                                                                                                                        //    two points to step through
  22.     real            sideA, sideB;                                                                                                                //    the classification of ptA and ptB respectively
  23.     hclass        poly_class = ON;                                                                                                        //    assume plane_3d and polygon coincident for starters
  24.     ptA = poly->Vertex (poly->Count () - 1);                                                                            //    start with the last point_3d
  25.     sideA = ptA | plane;                                                                                                                    //    classify it relative to the plane_3d
  26.     for (short i = -1; ++i < poly->Count ();)                                                                            //    loop on the points
  27.     {                                                                                                                                                            //    begin
  28.         ptB = poly->Vertex (i);                                                                                                            //    get the current point_3d
  29.         sideB = ptB | plane;                                                                                                                //    classify it relative to the plane_3d
  30.         if (sideB > EPSILON)                                                                                                                //    if the current point_3d is on the positive side
  31.         {                                                                                                                                                        //    begin
  32.             if (poly_class == ON)                                                                                                            //    if the polygon classification is on
  33.                 poly_class = OUT;                                                                                                                //    classify the polygon as out
  34.             else if (poly_class != OUT)                                                                                                //    else if the polygon classification is not out
  35.                 poly_class = SPANNING;                                                                                                    //    set the polygon classification to spanning
  36.             if (sideA < -EPSILON)                                                                                                            //    if the previous point_3d was on the opposite side of the plane_3d
  37.             {                                                                                                                                                    //    begin
  38.                 vector_3d    v = ptB - ptA;                                                                                                //    compute the vector_3d between the points
  39.                 outpts[out_c++] = inpts[in_c++] =                                                                             //    add the newly computed point_3d to the partitions
  40.                     ptA + (v * (-(ptA | plane) / (v | plane)));                                                        //    add the newly computed point_3d to the partitions
  41.                 poly_class = SPANNING;                                                                                                    //    set the poly_class appropriately
  42.             }                                                                                                                                                    //    end
  43.             outpts[out_c++] = ptB;                                                                                                        //    add the current point_3d to the positive partition
  44.         }                                                                                                                                                        //    end
  45.         else if (sideB < -EPSILON)                                                                                                    //    the current point_3d is on the negative side
  46.         {                                                                                                                                                        //    begin
  47.             if (poly_class == ON)                                                                                                            //    if the polygon classification is on
  48.                 poly_class = IN;                                                                                                                //    classify the polygon as in
  49.             else if (poly_class != IN)                                                                                                //    else if the polygon classification is not in
  50.                 poly_class = SPANNING;                                                                                                    //    set the polygon classification to spanning
  51.             if (sideA > EPSILON)                                                                                                            //    if the previous point_3d was on the opposite side of the plane_3d
  52.             {                                                                                                                                                    //    begin
  53.                 vector_3d    v = ptB - ptA;                                                                                                //    compute the vector_3d between the points
  54.                 outpts[out_c++] = inpts[in_c++] =                                                                             //    add the newly computed point_3d to the partitions
  55.                     ptA + (v * (-(ptA | plane) / (v | plane)));                                                        //    add the newly computed point_3d to the partitions
  56.                 poly_class = SPANNING;                                                                                                    //    set the poly_class appropriately
  57.             }                                                                                                                                                    //    end
  58.             inpts[in_c++] = ptB;                                                                                                            //    add the current point_3d to the negative partition
  59.         }                                                                                                                                                        //    end
  60.         else                                                                                                                                                //    the current point_3d is on the plane_3d
  61.             outpts[out_c++] = inpts[in_c++] = ptB;                                                                        //    add the current point_3d to the partitions
  62.         ptA = ptB;                                                                                                                                    //    copy the current point_3d to the last point_3d
  63.         sideA = sideB;                                                                                                                            //    copy the current point_3d's side information...
  64.     }                                                                                                                                                            //    end
  65.     switch (poly_class)                                                                                                                        //    perform the appropriate action based on the classification
  66.     {                                                                                                                                                            //    begin
  67.         case OUT:                                                                                                                                        //    if the polygon is entirely positive
  68.             out = poly;                                                                                                                                //    make the positive partition
  69.             break;                                                                                                                                        //    end positive
  70.         case IN:                                                                                                                                        //    if the polygon is entirely negative
  71.             in = poly;                                                                                                                                //    make the negative partition
  72.             break;                                                                                                                                        //    end negative
  73.         case SPANNING:                                                                                                                            //    if the polygon was plane
  74.             out = new rcpolygon (outpts, out_c);                                                                            //    make the positive partition
  75.             in = new rcpolygon (inpts, in_c);                                                                                    //    make the negative partition
  76.             break;                                                                                                                                        //    end spanning
  77.     }                                                                                                                                                            //    end
  78.     return poly_class;                                                                                                                        //    return the classification
  79. }                                                                                                                                                                //    end
  80.  
  81. //------------------------------------------------------------------------------
  82.